<?php
include_once "mailer/class.phpmailer.php";
class Gi_Mail {
  
  private $_mail = null;
  
  public function __construct ($from,$host,$name=null,$auth=true,$user=null,$pass=null) {
    $this->_mail = new PHPMailer();
    $this->_mail->PluginDir = "mailer/";
    $this->_mail->IsSMTP();
    $this->_mail->Host = $host;
    
    if ($auth) {
      $this->_mail->SMTPAuth = true;
      $this->_mail->Username = $user;
      $this->_mail->Password = $pass;
    }
    
    $this->_mail->From = $from;
    $this->_mail->FromName = $name;
  }
  
  public function add_addr($addr, $name = "", $type = "to") {
    strtolower($type);
    switch ($type) {
      case "to" :
        return $this->_mail->AddAddress($addr, $name);                    
        break;
      case "cc" :
        $this->_mail->AddCC($addr, $name);                                        
        break;
      case "bcc" :
        $this->_mail->AddBCC($addr, $name);                                   
        break;
      case "replyto": 
		    $this->_mail->AddReplyTo($addr,$name);
      default :
        throw new Gi_Exception("el tipo solo puede ser o to o cc o bcc");
    }
  }
  
  public function attach ($path,$name) {
    return $this->_mail->AddAttachment($path,$name);
  }
  
  public function set_messaje ($subject = null, $body = null) {
    $this->_mail->Subject = $subject;
    $this->_mail->Body = $body;;
  }
  
  public function send () {
    if ($this->_mail->Send()) {
      return true;
    }
    else {
      var_dump($this->_mail->ErrorInfo);
      throw new Gi_Exception($this->_mail->ErrorInfo);
    }
  }
}